
<template>
  <div class="container-box">
    <p>资产设备管理：维修管理</p>
    <!--筛选表单-->
    <div class="filter-form">
      <el-form ref="form" label-width="120px" :inline="true" size="small" label-position="top">
        <el-row type="flex" justify="space-between">
          <el-form-item>
            <div>小区名称</div>
            <SelectView v-model="queryData.community_code" />
          </el-form-item>
          <el-form-item>
            <div>设备名称</div>
            <el-input v-model="queryData.device_code" placeholder="请输入" />
          </el-form-item>
          <div>
            <el-form-item>
              <div class="div">最后维修</div>
              <el-date-picker
                v-model="queryData.last_maintain_time"
                style="width:200px"
                type="daterange"
                range-separator="至"
                start-placeholder="请选择"
                end-placeholder="请选择"
              />
            </el-form-item>
          </div>
          <div>
            <el-form-item>
              <div class="div">下次维修</div>
              <el-date-picker
                v-model="queryData.next_maintain_time"
                style="width:200px"
                type="daterange"
                range-separator="至"
                start-placeholder="请选择"
                end-placeholder="请选择"
              />
            </el-form-item>
          </div>
          <el-form-item>
            <div>操作</div>
            <el-button @click="onSearchCancel">重置</el-button>
            <el-button type="primary" @click="onSearchSubmit">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!--操作区域-->
    <el-row type="flex" justify="space-between" class="tool-bar">
      <div />
      <div>
        <el-button type="primary" size="small" plain @click="dialogShow = true">新增通知公告</el-button>
      </div>
    </el-row>
    <!--表单区域-->
    <div class="table-box">
      <el-table :data="list" border stripe>
        <el-table-column prop="community.name" label="小区名称" />
        <el-table-column prop="device.name" label="设备名称" />
        <el-table-column prop="community_code" label="设备编码" />
        <el-table-column prop="unit" label="维修单位" />
        <el-table-column prop="contacts" label="维修人" />
        <el-table-column label="照片">
          <template slot-scope="{row}">
            <el-image v-if="row.photo" width="60px" :preview-src-list="srcList" :src="$webURL + row.photo" alt="" />
            <el-image
              v-else
              width="60px"
              src="http://124.223.14.236:8881/public/dist/static/loadfail.85045efc.png"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="contacts_tel" label="联系方式" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="last_maintain_time" label="最后维修时间">
          <template slot-scope="{row}">
            <span>{{ moment(row.last_maintain_time) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="next_maintain_time" label="下次维修时间">
          <template slot-scope="{row}">
            <span>{{ moment(row.next_maintain_time) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button size="small" type="primary" icon="el-icon-edit" circle @click="editRow(row.id)" />
            <el-popconfirm title="确定删除吗？" @confirm="delRow(row.id)">
              <el-button slot="reference" size="small" type="danger" icon="el-icon-delete" circle />
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页区域-->
    <el-row type="flex" justify="space-between" class="tool-bar">
      <div />
      <div class="page">
        <el-pagination
          :current-page.sync="queryData.current"
          :page-sizes="[4, 8, 10, 20]"
          :page-size="queryData.pageSize"
          layout="total, sizes, prev, pager, next,jumper"
          :total="total"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-row>
    <!-- 弹框区域 -->
    <el-drawer title="车位使用信息" size="40%" :visible.sync="dialogShow" @close="btnCancel">
      <div class="form_flex">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="130px" inline size="normal">
          <el-row>
            <el-form-item label="所属小区" prop="community_code">
              <SelectDevice v-model="formData.community_code" :options="options" @change="onChange" />
            </el-form-item>
            <el-form-item label="设备名称" prop="device_code">
              <SelectDevice v-model="formData.device_code" :options="assets" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="维修单位" prop="unit">
              <el-input v-model="formData.unit" />
            </el-form-item>
            <el-form-item label="维修人" prop="contacts">
              <el-input v-model="formData.contacts" />
            </el-form-item>
          </el-row>
          <el-row>
            <div>
              <el-form-item label="联系方式" prop="contacts_tel">
                <el-input v-model="formData.contacts_tel" />
              </el-form-item>
            </div>
            <div>
              <el-form-item label="最后维修时间" prop="last_maintain_time">
                <template>
                  <el-date-picker v-model="formData.last_maintain_time" type="date" placeholder="选择日期" @change="change" />
                </template>
              </el-form-item>
            </div>
          </el-row>
          <el-row>
            <el-form-item label="下次维修时间" prop="next_maintain_time">
              <template>
                <el-date-picker v-model="formData.next_maintain_time" type="date" placeholder="选择日期" @change="change" />
              </template>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" type="textarea" />
            </el-form-item>
          </el-row>
          <el-form-item label="售后照片" prop="photo">
            <UploadImage v-model="formData.photo" />
          </el-form-item>
          <el-row type="flex" justify="end">
            <el-button size="small" type="primary" @click="btnOk">提交</el-button>
            <el-button size="small" @click="dialogShow = false">取消</el-button>
          </el-row>

        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { addMainApi, delMainApi, editMainApi, getMainListApi, editMainId } from '@/api/assets_maintain'
import SelectView from '../components/selectView.vue'
import SelectDevice from '../components/selectdevice.vue'
import UploadImage from '../components/uploadImage.vue'
import moment from 'moment'
import { getActivityPlotAPI, getlist } from '@/api/tankuang'
export default {
  name: 'MaintainApi',
  components: { SelectView, UploadImage, SelectDevice },
  data() {
    return {
      options: [],
      assets: [],
      fileList: [],
      list: [],
      formData: {
        unit: '', // 维修单位
        contacts: '', // 维修人
        photo: '', // 照片
        contacts_tel: null, // 联系方式
        remark: '', // 备注
        last_maintain_time: '', // 最后维修时间
        next_maintain_time: '' // 下次维修时间
      },
      queryData: {
        current: 1,
        pageSize: 5,
        next_maintain_time: null,
        last_maintain_time: null,
        community_code: ''
      },
      formRules: {

      },
      total: 0,
      dialogShow: false,
      time: '',
      msg: ''
    }
  },
  created() {
    this.initData()
    this.getActivityPlot()
  },

  methods: {
    change(val) {
      this.formData.last_maintain_time = moment(val).valueOf()
      this.formData.next_maintain_time = moment(val).valueOf()
    },
    async getActivityPlot() {
      const res = await getActivityPlotAPI()
      this.options = res.data
    },
    async onChange(val) {
      const res = await getlist(val)
      this.assets = res.data
      // console.log(res);
    },
    // 获取页面数据
    async initData() {
      const res = await getMainListApi(this.queryData)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.count
      this.msg = res.msg
      this.srcList = res.data.rows.map(item => 'http://124.223.14.236:8881' + item.photo)
    },
    // 分页
    sizeChange(val) {
      this.queryData.pageSize = val
      this.initData()
    },
    currentChange(val) {
      this.queryData.current = val
      this.initData()
    },
    // 重置
    onSearchCancel() {
      this.queryData = {
        device_code: '',
        current: 1,
        pageSize: 5,
        next_maintain_time: null,
        last_maintain_time: null,
        community_code: ''
      }
      // this.$message.success('重置成功')
      this.initData()
    },
    // 查询
    onSearchSubmit() {
      this.queryData.current = 1
      this.initData()
    },
    // 回显
    async editRow(id) {
      // this.$refs.form.validate()
      const res = await editMainId(id)
      // console.log(res);
      this.formData = res.data
      this.dialogShow = true
    },
    // 关闭弹框
    btnCancel() {
      this.formData = {
        unit: '', // 维修单位
        contacts: '', // 维修人
        photo: '', // 照片
        contacts_tel: null, // 联系方式
        remark: '', // 备注
        last_maintain_time: '', // 最后维修时间
        next_maintain_time: '' // 下次维修时间
      }
      this.$refs.form.clearValidate()
    },
    // 删除
    async delRow(id) {
      // console.log(111111);
      await delMainApi(id)
      this.$message.success(this.msg)
      if (this.queryData.current > 1 && this.list.length === 1) {
        this.queryData.current--
      }
      this.initData()
    },
    // 提交
    async btnOk() {
      await this.$refs.form.validate()
      this.formData.id ? await editMainApi(this.formData) : await addMainApi(this.formData)
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      this.dialogShow = false
      this.initData()
    },
    // 时间转化
    moment(time) {
      return moment(time).format('YYYY/MM/DD hh:mm:ss')
    }
  }
}
</script>
<style scoped>
.form_flex .el-row {
  display: flex;
}

.container-box {
  padding: 30px;
}

.filter-form,
.tool-bar {
  border: 1px solid #ddd;
  padding: 15px;
  margin-bottom: 10px;
}

.filter-form .el-form-item {
  width: 15%;
}

.el-input {
  width: 200px;
}

.el-select {
  width: 200px;
}
.div{
  width: 200px;
}
.el-drawer__title{
  padding: 0 30px;
}
</style>

